

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
  "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">


<html xmlns="http://www.w3.org/1999/xhtml">
  <head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    
    <title>ALASpy.geotif &mdash; ALASpy 0.1.1 documentation</title>
    
    <link rel="stylesheet" href="../../_static/default.css" type="text/css" />
    <link rel="stylesheet" href="../../_static/pygments.css" type="text/css" />
    
    <script type="text/javascript">
      var DOCUMENTATION_OPTIONS = {
        URL_ROOT:    '../../',
        VERSION:     '0.1.1',
        COLLAPSE_INDEX: false,
        FILE_SUFFIX: '.html',
        HAS_SOURCE:  true
      };
    </script>
    <script type="text/javascript" src="../../_static/jquery.js"></script>
    <script type="text/javascript" src="../../_static/underscore.js"></script>
    <script type="text/javascript" src="../../_static/doctools.js"></script>
    <script type="text/javascript" src="../../_static/sidebar.js"></script>
    <link rel="top" title="ALASpy 0.1.1 documentation" href="../../index.html" />
    <link rel="up" title="Module code" href="../index.html" /> 
  </head>
  <body>
    <div class="related">
      <h3>Navigation</h3>
      <ul>
        <li class="right" style="margin-right: 10px">
          <a href="../../genindex.html" title="General Index"
             accesskey="I">index</a></li>
        <li class="right" >
          <a href="../../py-modindex.html" title="Python Module Index"
             >modules</a> |</li>
        <li><a href="../../index.html">ALASpy 0.1.1 documentation</a> &raquo;</li>
          <li><a href="../index.html" accesskey="U">Module code</a> &raquo;</li> 
      </ul>
    </div>  

    <div class="document">
      <div class="documentwrapper">
        <div class="bodywrapper">
          <div class="body">
            
  <h1>Source code for ALASpy.geotif</h1><div class="highlight"><pre>
<span class="c"># -*- coding: utf-8 -*-</span>
<span class="sd">&quot;&quot;&quot; A Python interface module to some GDAL library functions to translate</span>
<span class="sd">    LAS Variable Record content (Geotiff keys) into WKT or Proj4 strings</span>

<span class="sd">    :Author:</span>
<span class="sd">      - 20120816-20120929 Roberto Vidmar</span>

<span class="sd">    :Revision:  $Revision: 2 $</span>
<span class="sd">                $Date: 2012-10-22 15:55:02 +0200 (Mon, 22 Oct 2012) $</span>

<span class="sd">    :Copyright: 2012-2012</span>
<span class="sd">                Nicola Creati &lt;ncreati@inogs.it&gt;</span>
<span class="sd">                Roberto Vidmar &lt;rvidmar@inogs.it&gt;</span>

<span class="sd">    :License: MIT/X11 License (see :download:`LICENSE.txt</span>
<span class="sd">                               &lt;../../LICENSE.txt&gt;`)</span>
<span class="sd">&quot;&quot;&quot;</span>

<span class="kn">import</span> <span class="nn">ctypes</span>
<span class="kn">import</span> <span class="nn">sys</span>
<span class="kn">import</span> <span class="nn">struct</span>
<span class="kn">from</span> <span class="nn">lasExceptions</span> <span class="kn">import</span> <span class="n">ConversionFailed</span><span class="p">,</span> <span class="n">UnimplementedException</span>

<span class="n">SHORT</span> <span class="o">=</span> <span class="s">&#39;h&#39;</span>
<span class="n">DOUBLE</span> <span class="o">=</span> <span class="s">&#39;d&#39;</span>

<span class="c"># Tag types (from geotiff.h)</span>
<span class="n">TAGTYPE_BYTE</span>     <span class="o">=</span> <span class="mi">1</span>
<span class="n">TAGTYPE_SHORT</span>    <span class="o">=</span> <span class="mi">2</span>
<span class="n">TAGTYPE_LONG</span>     <span class="o">=</span> <span class="mi">3</span>
<span class="n">TAGTYPE_RATIONAL</span> <span class="o">=</span> <span class="mi">4</span>
<span class="n">TAGTYPE_ASCII</span>    <span class="o">=</span> <span class="mi">5</span>
<span class="n">TAGTYPE_FLOAT</span>    <span class="o">=</span> <span class="mi">6</span>
<span class="n">TAGTYPE_DOUBLE</span>   <span class="o">=</span> <span class="mi">7</span>
<span class="n">TAGTYPE_SBYTE</span>    <span class="o">=</span> <span class="mi">8</span>
<span class="n">TAGTYPE_SSHORT</span>   <span class="o">=</span> <span class="mi">9</span>
<span class="n">TAGTYPE_SLONG</span>    <span class="o">=</span> <span class="mi">10</span>
<span class="n">TAGTYPE_UNKNOWN</span>  <span class="o">=</span> <span class="mi">11</span>

<span class="n">pinfo_t</span> <span class="o">=</span> <span class="n">ctypes</span><span class="o">.</span><span class="n">c_ushort</span>

<span class="c">#===============================================================================</span>
<div class="viewcode-block" id="GTIFDefn"><a class="viewcode-back" href="../../ALASpy.html#ALASpy.geotif.GTIFDefn">[docs]</a><span class="k">class</span> <span class="nc">GTIFDefn</span><span class="p">(</span><span class="n">ctypes</span><span class="o">.</span><span class="n">Structure</span><span class="p">):</span>
  <span class="n">MAX_GTIF_PROJPARMS</span> <span class="o">=</span> <span class="mi">10</span>
  <span class="n">_fields_</span> <span class="o">=</span> <span class="p">[</span>
    <span class="p">(</span><span class="s">&#39;Model&#39;</span><span class="p">,</span>             <span class="n">ctypes</span><span class="o">.</span><span class="n">c_short</span><span class="p">),</span>
    <span class="p">(</span><span class="s">&#39;PCS&#39;</span><span class="p">,</span>               <span class="n">ctypes</span><span class="o">.</span><span class="n">c_short</span><span class="p">),</span>
    <span class="p">(</span><span class="s">&#39;GCS&#39;</span><span class="p">,</span>               <span class="n">ctypes</span><span class="o">.</span><span class="n">c_short</span><span class="p">),</span>
    <span class="p">(</span><span class="s">&#39;UOMLength&#39;</span><span class="p">,</span>         <span class="n">ctypes</span><span class="o">.</span><span class="n">c_short</span><span class="p">),</span>
    <span class="p">(</span><span class="s">&#39;UOMLengthInMeters&#39;</span><span class="p">,</span> <span class="n">ctypes</span><span class="o">.</span><span class="n">c_double</span><span class="p">),</span>
    <span class="p">(</span><span class="s">&#39;UOMAngle&#39;</span><span class="p">,</span>          <span class="n">ctypes</span><span class="o">.</span><span class="n">c_short</span><span class="p">),</span>
    <span class="p">(</span><span class="s">&#39;UOMAngleInDegrees&#39;</span><span class="p">,</span> <span class="n">ctypes</span><span class="o">.</span><span class="n">c_double</span><span class="p">),</span>
    <span class="p">(</span><span class="s">&#39;Datum&#39;</span><span class="p">,</span>             <span class="n">ctypes</span><span class="o">.</span><span class="n">c_short</span><span class="p">),</span>
    <span class="p">(</span><span class="s">&#39;PM&#39;</span><span class="p">,</span>                <span class="n">ctypes</span><span class="o">.</span><span class="n">c_short</span><span class="p">),</span>
    <span class="p">(</span><span class="s">&#39;PMLongToGreenwich&#39;</span><span class="p">,</span> <span class="n">ctypes</span><span class="o">.</span><span class="n">c_double</span><span class="p">),</span>
    <span class="p">(</span><span class="s">&#39;Ellipsoid&#39;</span><span class="p">,</span>         <span class="n">ctypes</span><span class="o">.</span><span class="n">c_short</span><span class="p">),</span>
    <span class="p">(</span><span class="s">&#39;SemiMajor&#39;</span><span class="p">,</span>         <span class="n">ctypes</span><span class="o">.</span><span class="n">c_double</span><span class="p">),</span>
    <span class="p">(</span><span class="s">&#39;SemiMinor&#39;</span><span class="p">,</span>         <span class="n">ctypes</span><span class="o">.</span><span class="n">c_double</span><span class="p">),</span>
    <span class="p">(</span><span class="s">&#39;TOWGS84Count&#39;</span><span class="p">,</span>      <span class="n">ctypes</span><span class="o">.</span><span class="n">c_short</span><span class="p">),</span>
    <span class="p">(</span><span class="s">&#39;TOWGS84&#39;</span><span class="p">,</span>           <span class="n">ctypes</span><span class="o">.</span><span class="n">c_double</span> <span class="o">*</span> <span class="mi">7</span><span class="p">),</span>
    <span class="p">(</span><span class="s">&#39;ProjCode&#39;</span><span class="p">,</span>          <span class="n">ctypes</span><span class="o">.</span><span class="n">c_short</span><span class="p">),</span>
    <span class="p">(</span><span class="s">&#39;Projection&#39;</span><span class="p">,</span>        <span class="n">ctypes</span><span class="o">.</span><span class="n">c_short</span><span class="p">),</span>
    <span class="p">(</span><span class="s">&#39;CTProjection&#39;</span><span class="p">,</span>      <span class="n">ctypes</span><span class="o">.</span><span class="n">c_short</span><span class="p">),</span>
    <span class="p">(</span><span class="s">&#39;nParms&#39;</span><span class="p">,</span>            <span class="n">ctypes</span><span class="o">.</span><span class="n">c_int</span><span class="p">),</span>
    <span class="p">(</span><span class="s">&#39;ProjParm&#39;</span><span class="p">,</span>          <span class="n">ctypes</span><span class="o">.</span><span class="n">c_double</span> <span class="o">*</span> <span class="n">MAX_GTIF_PROJPARMS</span><span class="p">),</span>
    <span class="p">(</span><span class="s">&#39;ProjParmId&#39;</span><span class="p">,</span>        <span class="n">ctypes</span><span class="o">.</span><span class="n">c_int</span> <span class="o">*</span> <span class="n">MAX_GTIF_PROJPARMS</span><span class="p">),</span>
    <span class="p">(</span><span class="s">&#39;MapSys&#39;</span><span class="p">,</span>            <span class="n">ctypes</span><span class="o">.</span><span class="n">c_int</span><span class="p">),</span>
    <span class="p">(</span><span class="s">&#39;Zone&#39;</span><span class="p">,</span>              <span class="n">ctypes</span><span class="o">.</span><span class="n">c_int</span><span class="p">),</span>
    <span class="p">(</span><span class="s">&#39;DefnSet&#39;</span><span class="p">,</span>           <span class="n">ctypes</span><span class="o">.</span><span class="n">c_int</span><span class="p">),</span>
    <span class="p">]</span>

<div class="viewcode-block" id="GTIFDefn.__repr__"><a class="viewcode-back" href="../../ALASpy.html#ALASpy.geotif.GTIFDefn.__repr__">[docs]</a>  <span class="k">def</span> <span class="nf">__repr__</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
    <span class="sd">&quot;&quot;&quot; A representation of all fields of a :class:`GTIFDefn` instance</span>
<span class="sd">    &quot;&quot;&quot;</span>
    <span class="c"># I love strings!</span>
    <span class="n">s</span> <span class="o">=</span> <span class="s">&quot;</span><span class="si">%s</span><span class="s">:</span><span class="se">\n</span><span class="s">&quot;</span> <span class="o">%</span> <span class="bp">self</span><span class="o">.</span><span class="n">__class__</span><span class="o">.</span><span class="n">__name__</span>
    <span class="k">for</span> <span class="n">f</span> <span class="ow">in</span> <span class="bp">self</span><span class="o">.</span><span class="n">_fields_</span><span class="p">:</span>
      <span class="n">val</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">__getattribute__</span><span class="p">(</span><span class="n">f</span><span class="p">[</span><span class="mi">0</span><span class="p">])</span>
      <span class="n">p</span> <span class="o">=</span> <span class="nb">str</span><span class="p">(</span><span class="n">val</span><span class="p">)</span><span class="o">.</span><span class="n">partition</span><span class="p">(</span><span class="s">&#39;Array&#39;</span><span class="p">)[</span><span class="o">-</span><span class="mi">1</span><span class="p">]</span>
      <span class="k">if</span> <span class="n">p</span><span class="p">:</span>
        <span class="n">size</span> <span class="o">=</span> <span class="n">p</span><span class="o">.</span><span class="n">split</span><span class="p">()[</span><span class="mi">0</span><span class="p">]</span><span class="o">.</span><span class="n">strip</span><span class="p">(</span><span class="s">&#39;_&#39;</span><span class="p">)</span>
        <span class="n">s</span> <span class="o">+=</span> <span class="s">&quot;  </span><span class="si">%s</span><span class="s"> = </span><span class="si">%s</span><span class="se">\n</span><span class="s">&quot;</span> <span class="o">%</span> <span class="p">(</span><span class="n">f</span><span class="p">[</span><span class="mi">0</span><span class="p">],</span> <span class="nb">repr</span><span class="p">([</span><span class="n">val</span><span class="p">[</span><span class="n">i</span><span class="p">]</span> <span class="k">for</span> <span class="n">i</span> <span class="ow">in</span> <span class="nb">xrange</span><span class="p">(</span><span class="nb">int</span><span class="p">(</span><span class="n">size</span><span class="p">))]))</span>
      <span class="k">else</span><span class="p">:</span>
        <span class="n">s</span> <span class="o">+=</span> <span class="s">&quot;  </span><span class="si">%s</span><span class="s"> = </span><span class="si">%s</span><span class="se">\n</span><span class="s">&quot;</span> <span class="o">%</span> <span class="p">(</span><span class="n">f</span><span class="p">[</span><span class="mi">0</span><span class="p">],</span> <span class="n">val</span><span class="p">)</span>
    <span class="k">return</span> <span class="n">s</span>

<span class="c">#===============================================================================</span></div></div>
<div class="viewcode-block" id="TIFFMethod"><a class="viewcode-back" href="../../ALASpy.html#ALASpy.geotif.TIFFMethod">[docs]</a><span class="k">class</span> <span class="nc">TIFFMethod</span><span class="p">(</span><span class="n">ctypes</span><span class="o">.</span><span class="n">Structure</span><span class="p">):</span>
  <span class="n">_fields_</span> <span class="o">=</span> <span class="p">[</span>
    <span class="p">(</span><span class="s">&#39;get&#39;</span><span class="p">,</span> <span class="n">ctypes</span><span class="o">.</span><span class="n">c_void_p</span><span class="p">),</span>
    <span class="p">(</span><span class="s">&#39;set&#39;</span><span class="p">,</span> <span class="n">ctypes</span><span class="o">.</span><span class="n">c_void_p</span><span class="p">),</span>
    <span class="p">(</span><span class="s">&#39;type&#39;</span><span class="p">,</span> <span class="n">ctypes</span><span class="o">.</span><span class="n">c_void_p</span><span class="p">)]</span>

<span class="c">#===============================================================================</span></div>
<div class="viewcode-block" id="GeoKey"><a class="viewcode-back" href="../../ALASpy.html#ALASpy.geotif.GeoKey">[docs]</a><span class="k">class</span> <span class="nc">GeoKey</span><span class="p">(</span><span class="n">ctypes</span><span class="o">.</span><span class="n">Structure</span><span class="p">):</span>
  <span class="n">_fields_</span> <span class="o">=</span> <span class="p">[</span>
    <span class="p">(</span><span class="s">&#39;gk_key&#39;</span><span class="p">,</span> <span class="n">ctypes</span><span class="o">.</span><span class="n">c_int</span><span class="p">),</span>
    <span class="p">(</span><span class="s">&#39;gk_size&#39;</span><span class="p">,</span> <span class="n">ctypes</span><span class="o">.</span><span class="n">c_size_t</span><span class="p">),</span>
    <span class="p">(</span><span class="s">&#39;gk_type&#39;</span><span class="p">,</span> <span class="n">ctypes</span><span class="o">.</span><span class="n">c_int</span><span class="p">),</span>
    <span class="p">(</span><span class="s">&#39;gk_count&#39;</span><span class="p">,</span> <span class="n">ctypes</span><span class="o">.</span><span class="n">c_long</span><span class="p">),</span>
    <span class="c">#(&#39;gk_data&#39;, ctypes.c_char_p),</span>
    <span class="p">(</span><span class="s">&#39;gk_data&#39;</span><span class="p">,</span> <span class="n">ctypes</span><span class="o">.</span><span class="n">POINTER</span><span class="p">(</span><span class="n">ctypes</span><span class="o">.</span><span class="n">c_char</span><span class="p">)),</span>
    <span class="p">]</span>

<span class="c">#===============================================================================</span></div>
<div class="viewcode-block" id="GTiff"><a class="viewcode-back" href="../../ALASpy.html#ALASpy.geotif.GTiff">[docs]</a><span class="k">class</span> <span class="nc">GTiff</span><span class="p">(</span><span class="n">ctypes</span><span class="o">.</span><span class="n">Structure</span><span class="p">):</span>
  <span class="sd">&quot;&quot;&quot; The class to interface Python code with libgdal. We need to fill this</span>
<span class="sd">      structure to get back later a WKT string</span>
<span class="sd">  &quot;&quot;&quot;</span>
  <span class="n">GeoKeyDirectoryTag</span> <span class="o">=</span> <span class="mi">34735</span>
  <span class="n">GeoDoubleParamsTag</span> <span class="o">=</span> <span class="mi">34736</span>
  <span class="n">GeoAsciiParamsTag</span> <span class="o">=</span> <span class="mi">34737</span>

  <span class="n">_fields_</span> <span class="o">=</span> <span class="p">[</span>
    <span class="p">(</span><span class="s">&#39;gt_tif&#39;</span><span class="p">,</span> <span class="n">ctypes</span><span class="o">.</span><span class="n">c_void_p</span><span class="p">),</span>
    <span class="p">(</span><span class="s">&#39;gt_methods&#39;</span><span class="p">,</span> <span class="n">TIFFMethod</span><span class="p">),</span>
    <span class="p">(</span><span class="s">&#39;gt_flags&#39;</span><span class="p">,</span> <span class="n">ctypes</span><span class="o">.</span><span class="n">c_int</span><span class="p">),</span>
    <span class="p">(</span><span class="s">&#39;gt_version&#39;</span><span class="p">,</span> <span class="n">pinfo_t</span><span class="p">),</span>
    <span class="p">(</span><span class="s">&#39;gt_rev_major&#39;</span><span class="p">,</span> <span class="n">pinfo_t</span><span class="p">),</span>
    <span class="p">(</span><span class="s">&#39;gt_rev_minor&#39;</span><span class="p">,</span> <span class="n">pinfo_t</span><span class="p">),</span>
    <span class="p">(</span><span class="s">&#39;gt_num_keys&#39;</span><span class="p">,</span> <span class="n">ctypes</span><span class="o">.</span><span class="n">c_int</span><span class="p">),</span>
    <span class="p">(</span><span class="s">&#39;gt_keys&#39;</span><span class="p">,</span> <span class="n">ctypes</span><span class="o">.</span><span class="n">POINTER</span><span class="p">(</span><span class="n">GeoKey</span><span class="p">)),</span>
    <span class="p">(</span><span class="s">&#39;gt_keyindex&#39;</span><span class="p">,</span> <span class="n">ctypes</span><span class="o">.</span><span class="n">POINTER</span><span class="p">(</span><span class="n">ctypes</span><span class="o">.</span><span class="n">c_int</span><span class="p">)),</span>
    <span class="p">(</span><span class="s">&#39;gt_keymin&#39;</span><span class="p">,</span> <span class="n">ctypes</span><span class="o">.</span><span class="n">c_int</span><span class="p">),</span>
    <span class="p">(</span><span class="s">&#39;gt_keymax&#39;</span><span class="p">,</span> <span class="n">ctypes</span><span class="o">.</span><span class="n">c_int</span><span class="p">),</span>
    <span class="p">(</span><span class="s">&#39;gt_short&#39;</span><span class="p">,</span> <span class="n">ctypes</span><span class="o">.</span><span class="n">POINTER</span><span class="p">(</span><span class="n">pinfo_t</span><span class="p">)),</span>
    <span class="p">(</span><span class="s">&#39;gt_double&#39;</span><span class="p">,</span> <span class="n">ctypes</span><span class="o">.</span><span class="n">POINTER</span><span class="p">(</span><span class="n">ctypes</span><span class="o">.</span><span class="n">c_double</span><span class="p">)),</span>
    <span class="p">(</span><span class="s">&#39;gt_nshorts&#39;</span><span class="p">,</span> <span class="n">ctypes</span><span class="o">.</span><span class="n">c_int</span><span class="p">),</span>
    <span class="p">(</span><span class="s">&#39;gt_ndoubles&#39;</span><span class="p">,</span> <span class="n">ctypes</span><span class="o">.</span><span class="n">c_int</span><span class="p">)]</span>

<div class="viewcode-block" id="GTiff.__init__"><a class="viewcode-back" href="../../ALASpy.html#ALASpy.geotif.GTiff.__init__">[docs]</a>  <span class="k">def</span> <span class="nf">__init__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">geokeys</span><span class="p">,</span> <span class="n">geoAsciiParams</span><span class="o">=</span><span class="bp">None</span><span class="p">,</span> <span class="n">geoDoubleParams</span><span class="o">=</span><span class="bp">None</span><span class="p">):</span>
    <span class="sd">&quot;&quot;&quot; Create a new instance of the structure and populate it with data</span>
<span class="sd">        from Geotiff like &quot;geokeys&quot; tuple, geoAsciiParams string and</span>
<span class="sd">        geoDoubleParams tuple from LAS Variable Length Records.</span>

<span class="sd">        This is an example::</span>

<span class="sd">          [</span>
<span class="sd">          (1, 1, 0, 13),</span>
<span class="sd">          (1024, 0, 1, 1),</span>
<span class="sd">          (2048, 0, 1, 32767),</span>
<span class="sd">          (2050, 0, 1, 32767),</span>
<span class="sd">          (2056, 0, 1, 7030),</span>
<span class="sd">          (3072, 0, 1, 32767),</span>
<span class="sd">          (3074, 0, 1, 32767),</span>
<span class="sd">          (3075, 0, 1, 1),</span>
<span class="sd">          (3076, 0, 1, 9001),</span>
<span class="sd">          (3080, 34736, 1, 1),</span>
<span class="sd">          (3081, 34736, 1, 0),</span>
<span class="sd">          (3082, 34736, 1, 3),</span>
<span class="sd">          (3083, 34736, 1, 4),</span>
<span class="sd">          (3092, 34736, 1, 2)</span>
<span class="sd">          ]</span>
<span class="sd">          &quot;UTM Zone 15, Northern Hemisphere|NAD83|&quot;</span>
<span class="sd">          (0.0, -81.0, 0.9996, 500000.0, 0.0)</span>
<span class="sd">  &quot;&quot;&quot;</span>
    <span class="c">#for gk in geokeys:</span>
    <span class="c">#  print gk</span>
    <span class="c">#print geoDoubleParams</span>

    <span class="c"># Create an empty list of c_doubles, we will need the REFERENCE to them</span>
    <span class="c"># Do *NOT* delete the next statement</span>
    <span class="bp">self</span><span class="o">.</span><span class="n">c_doubles</span> <span class="o">=</span> <span class="p">[]</span>
    <span class="n">key0</span> <span class="o">=</span> <span class="n">geokeys</span><span class="p">[</span><span class="mi">0</span><span class="p">]</span>
    <span class="n">keys</span> <span class="o">=</span> <span class="p">[</span><span class="n">k</span><span class="p">[</span><span class="mi">0</span><span class="p">]</span> <span class="k">for</span> <span class="n">k</span> <span class="ow">in</span> <span class="n">geokeys</span><span class="p">][</span><span class="mi">1</span><span class="p">:]</span>
    <span class="bp">self</span><span class="o">.</span><span class="n">gt_methods</span> <span class="o">=</span> <span class="n">TIFFMethod</span><span class="p">()</span>
    <span class="bp">self</span><span class="o">.</span><span class="n">gt_short</span> <span class="o">=</span> <span class="p">(</span><span class="n">pinfo_t</span> <span class="o">*</span> <span class="p">(</span><span class="nb">len</span><span class="p">(</span><span class="n">geokeys</span><span class="p">)</span> <span class="o">*</span> <span class="mi">4</span><span class="p">))()</span>
    <span class="bp">self</span><span class="o">.</span><span class="n">gt_version</span> <span class="o">=</span> <span class="n">key0</span><span class="p">[</span><span class="mi">0</span><span class="p">]</span>
    <span class="bp">self</span><span class="o">.</span><span class="n">gt_rev_major</span> <span class="o">=</span> <span class="n">key0</span><span class="p">[</span><span class="mi">1</span><span class="p">]</span>
    <span class="bp">self</span><span class="o">.</span><span class="n">gt_rev_minor</span> <span class="o">=</span> <span class="n">key0</span><span class="p">[</span><span class="mi">2</span><span class="p">]</span>
    <span class="bp">self</span><span class="o">.</span><span class="n">gt_num_keys</span> <span class="o">=</span> <span class="n">key0</span><span class="p">[</span><span class="mi">3</span><span class="p">]</span>
    <span class="bp">self</span><span class="o">.</span><span class="n">gt_keys</span> <span class="o">=</span> <span class="p">(</span><span class="n">GeoKey</span> <span class="o">*</span> <span class="nb">len</span><span class="p">(</span><span class="n">geokeys</span><span class="p">))()</span>
    <span class="bp">self</span><span class="o">.</span><span class="n">gt_keymin</span> <span class="o">=</span> <span class="nb">min</span><span class="p">(</span><span class="n">keys</span><span class="p">)</span>
    <span class="bp">self</span><span class="o">.</span><span class="n">gt_keymax</span> <span class="o">=</span> <span class="nb">max</span><span class="p">(</span><span class="n">keys</span><span class="p">)</span>
    <span class="bp">self</span><span class="o">.</span><span class="n">gt_nshorts</span> <span class="o">=</span> <span class="p">(</span><span class="nb">len</span><span class="p">(</span><span class="n">geokeys</span><span class="p">)</span> <span class="o">*</span> <span class="mi">4</span><span class="p">)</span> <span class="c"># Every key has four shorts</span>

    <span class="c"># Create an array of 32768 elements to find the geokey tuple</span>
    <span class="c"># from its key: 32767 mean unknown *AND* INDEX 0 means key not found! </span>
    <span class="bp">self</span><span class="o">.</span><span class="n">gt_keyindex</span> <span class="o">=</span> <span class="p">(</span><span class="n">ctypes</span><span class="o">.</span><span class="n">c_int</span> <span class="o">*</span> <span class="mi">32768</span><span class="p">)()</span>

    <span class="k">if</span> <span class="n">geoDoubleParams</span> <span class="ow">is</span> <span class="bp">None</span><span class="p">:</span>
      <span class="bp">self</span><span class="o">.</span><span class="n">gt_double</span> <span class="o">=</span> <span class="p">(</span><span class="n">ctypes</span><span class="o">.</span><span class="n">c_double</span> <span class="o">*</span> <span class="mi">1</span><span class="p">)()</span>
      <span class="bp">self</span><span class="o">.</span><span class="n">gt_ndoubles</span> <span class="o">=</span> <span class="mi">0</span>
    <span class="k">else</span><span class="p">:</span>
      <span class="bp">self</span><span class="o">.</span><span class="n">gt_double</span> <span class="o">=</span> <span class="p">(</span><span class="n">ctypes</span><span class="o">.</span><span class="n">c_double</span> <span class="o">*</span> <span class="nb">len</span><span class="p">(</span><span class="n">geoDoubleParams</span><span class="p">))()</span>
      <span class="bp">self</span><span class="o">.</span><span class="n">gt_ndoubles</span> <span class="o">=</span> <span class="nb">len</span><span class="p">(</span><span class="n">geoDoubleParams</span><span class="p">)</span>

      <span class="k">for</span> <span class="n">i</span><span class="p">,</span> <span class="n">d</span> <span class="ow">in</span> <span class="nb">enumerate</span><span class="p">(</span><span class="n">geoDoubleParams</span><span class="p">):</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">gt_double</span><span class="p">[</span><span class="n">i</span><span class="p">]</span> <span class="o">=</span> <span class="n">ctypes</span><span class="o">.</span><span class="n">c_double</span><span class="p">(</span><span class="n">d</span><span class="p">)</span>

    <span class="n">geoKeysFlattened</span> <span class="o">=</span> <span class="nb">sum</span><span class="p">(</span><span class="n">geokeys</span><span class="p">,</span> <span class="p">())</span>
    <span class="k">for</span> <span class="n">i</span><span class="p">,</span> <span class="n">v</span> <span class="ow">in</span> <span class="nb">enumerate</span><span class="p">(</span><span class="n">geoKeysFlattened</span><span class="p">):</span>
      <span class="bp">self</span><span class="o">.</span><span class="n">gt_short</span><span class="p">[</span><span class="n">i</span><span class="p">]</span> <span class="o">=</span> <span class="n">ctypes</span><span class="o">.</span><span class="n">c_ushort</span><span class="p">(</span><span class="n">v</span><span class="p">)</span>

    <span class="k">for</span> <span class="n">j</span><span class="p">,</span> <span class="n">key</span> <span class="ow">in</span> <span class="nb">enumerate</span><span class="p">(</span><span class="n">geokeys</span><span class="p">[</span><span class="mi">1</span><span class="p">:]):</span>
      <span class="n">i</span> <span class="o">=</span> <span class="n">j</span> <span class="o">+</span> <span class="mi">1</span> <span class="c"># AAAAARGH!!!!!</span>
      <span class="bp">self</span><span class="o">.</span><span class="n">gt_keys</span><span class="p">[</span><span class="n">i</span><span class="p">]</span><span class="o">.</span><span class="n">gk_key</span> <span class="o">=</span> <span class="n">key</span><span class="p">[</span><span class="mi">0</span><span class="p">]</span>
      <span class="bp">self</span><span class="o">.</span><span class="n">gt_keyindex</span><span class="p">[</span><span class="n">key</span><span class="p">[</span><span class="mi">0</span><span class="p">]]</span> <span class="o">=</span> <span class="n">i</span>
      <span class="k">if</span> <span class="n">key</span><span class="p">[</span><span class="mi">1</span><span class="p">]</span> <span class="o">==</span> <span class="mi">0</span><span class="p">:</span>
        <span class="c"># Data type is unsigned short</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">gt_keys</span><span class="p">[</span><span class="n">i</span><span class="p">]</span><span class="o">.</span><span class="n">gk_size</span> <span class="o">=</span> <span class="mi">2</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">gt_keys</span><span class="p">[</span><span class="n">i</span><span class="p">]</span><span class="o">.</span><span class="n">gk_type</span> <span class="o">=</span> <span class="n">TAGTYPE_SHORT</span>
        <span class="n">ptr</span> <span class="o">=</span> <span class="n">key</span><span class="p">[</span><span class="mi">3</span><span class="p">]</span>
      <span class="k">elif</span> <span class="n">key</span><span class="p">[</span><span class="mi">1</span><span class="p">]</span> <span class="o">==</span> <span class="n">GTiff</span><span class="o">.</span><span class="n">GeoDoubleParamsTag</span><span class="p">:</span>
        <span class="c"># Data type is double</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">gt_keys</span><span class="p">[</span><span class="n">i</span><span class="p">]</span><span class="o">.</span><span class="n">gk_size</span> <span class="o">=</span> <span class="mi">8</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">gt_keys</span><span class="p">[</span><span class="n">i</span><span class="p">]</span><span class="o">.</span><span class="n">gk_type</span> <span class="o">=</span> <span class="n">TAGTYPE_DOUBLE</span>
        <span class="c"># 3 days of HARD WORK for these three lines</span>
        <span class="n">d</span> <span class="o">=</span> <span class="n">geoDoubleParams</span><span class="p">[</span><span class="n">key</span><span class="p">[</span><span class="mi">3</span><span class="p">]]</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">c_doubles</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">d</span><span class="p">)</span>
        <span class="n">value</span> <span class="o">=</span> <span class="n">ctypes</span><span class="o">.</span><span class="n">c_double</span><span class="p">(</span><span class="n">d</span><span class="p">)</span>
        <span class="n">ptr</span> <span class="o">=</span> <span class="n">ctypes</span><span class="o">.</span><span class="n">pointer</span><span class="p">(</span><span class="n">value</span><span class="p">)</span>
      <span class="k">elif</span> <span class="n">key</span><span class="p">[</span><span class="mi">1</span><span class="p">]</span> <span class="o">==</span> <span class="n">GTiff</span><span class="o">.</span><span class="n">GeoAsciiParamsTag</span><span class="p">:</span>
        <span class="c"># Data type is ascii</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">gt_keys</span><span class="p">[</span><span class="n">i</span><span class="p">]</span><span class="o">.</span><span class="n">gk_size</span> <span class="o">=</span> <span class="mi">1</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">gt_keys</span><span class="p">[</span><span class="n">i</span><span class="p">]</span><span class="o">.</span><span class="n">gk_type</span> <span class="o">=</span> <span class="n">TAGTYPE_ASCII</span>
        <span class="c"># Remove null character</span>
        <span class="n">string</span> <span class="o">=</span> <span class="n">geoAsciiParams</span><span class="p">[</span><span class="n">key</span><span class="p">[</span><span class="mi">3</span><span class="p">]:</span> <span class="n">key</span><span class="p">[</span><span class="mi">3</span><span class="p">]</span> <span class="o">+</span> <span class="n">key</span><span class="p">[</span><span class="mi">2</span><span class="p">]</span> <span class="o">-</span> <span class="mi">1</span><span class="p">]</span>
        <span class="n">ptr</span> <span class="o">=</span> <span class="n">ctypes</span><span class="o">.</span><span class="n">create_string_buffer</span><span class="p">(</span><span class="n">string</span><span class="p">)</span>
      <span class="k">else</span><span class="p">:</span>
        <span class="k">raise</span> <span class="n">UnimplementedException</span><span class="p">(</span>
          <span class="s">&quot;wTIFFTagLocation value &#39;</span><span class="si">%d</span><span class="s">&#39; is not implemented.&quot;</span> <span class="o">%</span> <span class="n">key</span><span class="p">[</span><span class="mi">1</span><span class="p">])</span>

      <span class="bp">self</span><span class="o">.</span><span class="n">gt_keys</span><span class="p">[</span><span class="n">i</span><span class="p">]</span><span class="o">.</span><span class="n">gk_count</span> <span class="o">=</span> <span class="n">key</span><span class="p">[</span><span class="mi">2</span><span class="p">]</span>
      <span class="bp">self</span><span class="o">.</span><span class="n">gt_keys</span><span class="p">[</span><span class="n">i</span><span class="p">]</span><span class="o">.</span><span class="n">gk_data</span> <span class="o">=</span> <span class="n">ctypes</span><span class="o">.</span><span class="n">cast</span><span class="p">(</span><span class="n">ptr</span><span class="p">,</span> <span class="n">ctypes</span><span class="o">.</span><span class="n">POINTER</span><span class="p">(</span><span class="n">ctypes</span><span class="o">.</span><span class="n">c_char</span><span class="p">))</span>
</div>
<div class="viewcode-block" id="GTiff.__repr__"><a class="viewcode-back" href="../../ALASpy.html#ALASpy.geotif.GTiff.__repr__">[docs]</a>  <span class="k">def</span> <span class="nf">__repr__</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
    <span class="sd">&quot;&quot;&quot; Unambiguous representation of the GeoKeys, GeoDoubleParams and</span>
<span class="sd">        geoAsciiParams</span>
<span class="sd">    &quot;&quot;&quot;</span>
    <span class="n">s</span> <span class="o">=</span> <span class="s">&quot;</span><span class="si">%s</span><span class="s">:</span><span class="se">\n</span><span class="s">&quot;</span> <span class="o">%</span> <span class="bp">self</span><span class="o">.</span><span class="n">__class__</span><span class="o">.</span><span class="n">__name__</span>
    <span class="n">geoKeys</span><span class="p">,</span> <span class="n">geoDoubles</span><span class="p">,</span> <span class="n">geoAscii</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">geoKeys</span><span class="p">()</span>
    <span class="n">s</span> <span class="o">+=</span> <span class="s">&quot;Geokeys: (</span><span class="se">\n</span><span class="s">&quot;</span>
    <span class="k">for</span> <span class="n">gk</span> <span class="ow">in</span> <span class="n">geoKeys</span><span class="p">:</span>
      <span class="n">s</span> <span class="o">+=</span> <span class="s">&quot;  </span><span class="si">%s</span><span class="se">\n</span><span class="s">&quot;</span> <span class="o">%</span> <span class="nb">repr</span><span class="p">(</span><span class="n">gk</span><span class="p">)</span>
    <span class="n">s</span> <span class="o">+=</span> <span class="s">&quot;)</span><span class="se">\n</span><span class="s">geoDoubleParams:</span><span class="se">\n</span><span class="s">&quot;</span>
    <span class="n">s</span> <span class="o">+=</span> <span class="s">&quot;  </span><span class="si">%s</span><span class="se">\n</span><span class="s">&quot;</span> <span class="o">%</span> <span class="nb">repr</span><span class="p">(</span><span class="n">geoDoubles</span><span class="p">)</span>
    <span class="n">s</span> <span class="o">+=</span> <span class="s">&quot;geoAsciiParams:</span><span class="se">\n</span><span class="s">&quot;</span>
    <span class="n">s</span> <span class="o">+=</span> <span class="s">&quot;  </span><span class="si">%s</span><span class="se">\n</span><span class="s">&quot;</span> <span class="o">%</span> <span class="nb">repr</span><span class="p">(</span><span class="n">geoAscii</span><span class="p">)</span>
    <span class="k">return</span> <span class="n">s</span>

<span class="c">#-------------------------------------------------------------------------------</span>
<span class="c"># Public methods</span>
<span class="c">#-------------------------------------------------------------------------------</span>
</div>
<div class="viewcode-block" id="GTiff.geoKeys"><a class="viewcode-back" href="../../ALASpy.html#ALASpy.geotif.GTiff.geoKeys">[docs]</a>  <span class="k">def</span> <span class="nf">geoKeys</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
    <span class="sd">&quot;&quot;&quot; Return (GeoKeys, GeoDoubleParams, GeoAsciiParams) tuple</span>

<span class="sd">    :returns: (GeoKeys, GeoDoubleParams, GeoAsciiParams)</span>
<span class="sd">    :rtype: tuple</span>
<span class="sd">    &quot;&quot;&quot;</span>
    <span class="n">keys</span> <span class="o">=</span> <span class="p">[</span><span class="bp">self</span><span class="o">.</span><span class="n">gt_keys</span><span class="p">[</span><span class="n">i</span><span class="p">]</span><span class="o">.</span><span class="n">gk_key</span> <span class="k">for</span> <span class="n">i</span> <span class="ow">in</span> <span class="nb">xrange</span><span class="p">(</span><span class="mi">1</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">gt_num_keys</span><span class="p">)]</span>
    <span class="c"># Keys must be sorted first</span>
    <span class="n">keys</span><span class="o">.</span><span class="n">sort</span><span class="p">()</span>

    <span class="c"># Build the geoKeys</span>
    <span class="n">geoDoubleParams</span> <span class="o">=</span> <span class="p">()</span>
    <span class="n">geoAsciiParams</span> <span class="o">=</span> <span class="s">&#39;&#39;</span>
    <span class="n">geoKeys</span> <span class="o">=</span> <span class="p">((</span><span class="bp">self</span><span class="o">.</span><span class="n">gt_version</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">gt_rev_major</span><span class="p">,</span>
      <span class="bp">self</span><span class="o">.</span><span class="n">gt_rev_minor</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">gt_num_keys</span> <span class="o">-</span> <span class="mi">1</span><span class="p">),</span> <span class="p">)</span>

    <span class="k">for</span> <span class="n">k</span> <span class="ow">in</span> <span class="n">keys</span><span class="p">:</span>
      <span class="n">index</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">gt_keyindex</span><span class="p">[</span><span class="n">k</span><span class="p">]</span>
      <span class="n">key</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">gt_keys</span><span class="p">[</span><span class="n">index</span><span class="p">]</span>
      <span class="k">if</span> <span class="n">key</span><span class="o">.</span><span class="n">gk_type</span> <span class="o">==</span> <span class="n">TAGTYPE_DOUBLE</span><span class="p">:</span>
        <span class="n">double</span> <span class="o">=</span> <span class="n">struct</span><span class="o">.</span><span class="n">unpack</span><span class="p">(</span><span class="n">DOUBLE</span><span class="p">,</span> <span class="n">key</span><span class="o">.</span><span class="n">gk_data</span><span class="p">[:</span><span class="mi">8</span><span class="p">])[</span><span class="mi">0</span><span class="p">]</span>
        <span class="n">geoDoubleParams</span> <span class="o">+=</span> <span class="p">(</span><span class="n">double</span><span class="p">,</span> <span class="p">)</span>
        <span class="n">geoKeys</span> <span class="o">+=</span> <span class="p">((</span>
          <span class="n">key</span><span class="o">.</span><span class="n">gk_key</span><span class="p">,</span>
          <span class="n">GTiff</span><span class="o">.</span><span class="n">GeoDoubleParamsTag</span><span class="p">,</span>
          <span class="n">key</span><span class="o">.</span><span class="n">gk_count</span><span class="p">,</span>
          <span class="nb">len</span><span class="p">(</span><span class="n">geoDoubleParams</span><span class="p">)</span> <span class="o">-</span> <span class="mi">1</span><span class="p">),</span> <span class="p">)</span>
      <span class="k">elif</span> <span class="n">key</span><span class="o">.</span><span class="n">gk_type</span> <span class="o">==</span> <span class="n">TAGTYPE_SHORT</span><span class="p">:</span>
        <span class="c"># ANOTHER 3 DAYS OF CRAZY WORK TO UNDERSTAND THIS!</span>
        <span class="c"># Look at gdal-1.9.0/frmts/gtiff/libgeotiff/geo_get.c</span>
        <span class="n">s</span> <span class="o">=</span> <span class="n">ctypes</span><span class="o">.</span><span class="n">cast</span><span class="p">(</span>
          <span class="n">ctypes</span><span class="o">.</span><span class="n">pointer</span><span class="p">(</span><span class="n">key</span><span class="o">.</span><span class="n">gk_data</span><span class="p">),</span> <span class="n">ctypes</span><span class="o">.</span><span class="n">POINTER</span><span class="p">(</span><span class="n">ctypes</span><span class="o">.</span><span class="n">c_char</span><span class="p">))</span>
        <span class="n">short</span> <span class="o">=</span> <span class="n">struct</span><span class="o">.</span><span class="n">unpack</span><span class="p">(</span><span class="n">SHORT</span><span class="p">,</span> <span class="n">s</span><span class="p">[:</span><span class="mi">2</span><span class="p">])[</span><span class="mi">0</span><span class="p">]</span>
        <span class="n">geoKeys</span> <span class="o">+=</span> <span class="p">((</span>
          <span class="n">key</span><span class="o">.</span><span class="n">gk_key</span><span class="p">,</span>
          <span class="mi">0</span><span class="p">,</span>
          <span class="n">key</span><span class="o">.</span><span class="n">gk_count</span><span class="p">,</span>
          <span class="n">short</span><span class="p">),</span> <span class="p">)</span>
      <span class="k">elif</span> <span class="n">key</span><span class="o">.</span><span class="n">gk_type</span> <span class="o">==</span> <span class="n">TAGTYPE_ASCII</span><span class="p">:</span>
        <span class="n">ascData</span> <span class="o">=</span> <span class="n">key</span><span class="o">.</span><span class="n">gk_data</span><span class="p">[:</span><span class="n">key</span><span class="o">.</span><span class="n">gk_count</span><span class="p">]</span>
        <span class="n">geoKeys</span> <span class="o">+=</span> <span class="p">((</span>
          <span class="n">key</span><span class="o">.</span><span class="n">gk_key</span><span class="p">,</span>
          <span class="n">GTiff</span><span class="o">.</span><span class="n">GeoAsciiParamsTag</span><span class="p">,</span>
          <span class="n">key</span><span class="o">.</span><span class="n">gk_count</span><span class="p">,</span>
          <span class="nb">len</span><span class="p">(</span><span class="n">geoAsciiParams</span><span class="p">)),</span> <span class="p">)</span>
        <span class="n">geoAsciiParams</span> <span class="o">+=</span> <span class="n">ascData</span>

    <span class="k">return</span> <span class="n">geoKeys</span><span class="p">,</span> <span class="n">geoDoubleParams</span><span class="p">,</span> <span class="n">geoAsciiParams</span>

<span class="c">#===============================================================================</span></div></div>
<div class="viewcode-block" id="FILE"><a class="viewcode-back" href="../../ALASpy.html#ALASpy.geotif.FILE">[docs]</a><span class="k">class</span> <span class="nc">FILE</span><span class="p">(</span><span class="n">ctypes</span><span class="o">.</span><span class="n">Structure</span><span class="p">):</span>
  <span class="k">pass</span>
</div>
<span class="n">pFile</span> <span class="o">=</span> <span class="n">ctypes</span><span class="o">.</span><span class="n">POINTER</span><span class="p">(</span><span class="n">FILE</span><span class="p">)</span>

<span class="c">#===============================================================================</span>
<span class="c"># Link SOME GDAL Library functions</span>
<span class="c">#lib = ctypes.CDLL(&#39;C:\Python27\Lib\site-packages\osgeo\gdal19.dll&#39;)</span>
<span class="n">lib</span> <span class="o">=</span> <span class="n">ctypes</span><span class="o">.</span><span class="n">CDLL</span><span class="p">(</span><span class="s">&#39;/usr/local/lib/libgdal.so.1&#39;</span><span class="p">)</span>
<span class="c">#lib = ctypes.CDLL(&#39;/d0/Downloads/gdal-1.9.0-Bobo/.libs/libgdal.so.1&#39;)</span>

<span class="c"># Link GTIFGetDefn</span>
<span class="n">GTIFGetDefn</span> <span class="o">=</span> <span class="n">lib</span><span class="o">.</span><span class="n">GTIFGetDefn</span>
<span class="n">GTIFGetDefn</span><span class="o">.</span><span class="n">argtypes</span> <span class="o">=</span> <span class="p">[</span><span class="n">ctypes</span><span class="o">.</span><span class="n">POINTER</span><span class="p">(</span><span class="n">GTiff</span><span class="p">),</span> <span class="n">ctypes</span><span class="o">.</span><span class="n">POINTER</span><span class="p">(</span><span class="n">GTIFDefn</span><span class="p">)]</span>
<span class="n">GTIFGetDefn</span><span class="o">.</span><span class="n">restype</span> <span class="o">=</span> <span class="n">ctypes</span><span class="o">.</span><span class="n">c_int</span>

<span class="c"># Link GTIFPrintDefn</span>
<span class="n">GTIFPrintDefn</span> <span class="o">=</span> <span class="n">lib</span><span class="o">.</span><span class="n">GTIFPrintDefn</span>
<span class="n">GTIFPrintDefn</span><span class="o">.</span><span class="n">argtypes</span> <span class="o">=</span> <span class="p">[</span><span class="n">ctypes</span><span class="o">.</span><span class="n">POINTER</span><span class="p">(</span><span class="n">GTIFDefn</span><span class="p">),</span> <span class="n">pFile</span><span class="p">]</span>
<span class="n">GTIFPrintDefn</span><span class="o">.</span><span class="n">restype</span> <span class="o">=</span> <span class="n">ctypes</span><span class="o">.</span><span class="n">c_int</span>

<span class="c"># Link GTIFGetOGISDefn</span>
<span class="n">GTIFGetOGISDefn</span> <span class="o">=</span> <span class="n">lib</span><span class="o">.</span><span class="n">GTIFGetOGISDefn</span>
<span class="n">GTIFGetOGISDefn</span><span class="o">.</span><span class="n">argtypes</span> <span class="o">=</span> <span class="p">[</span><span class="n">ctypes</span><span class="o">.</span><span class="n">POINTER</span><span class="p">(</span><span class="n">GTiff</span><span class="p">),</span> <span class="n">ctypes</span><span class="o">.</span><span class="n">POINTER</span><span class="p">(</span><span class="n">GTIFDefn</span><span class="p">)]</span>
<span class="n">GTIFGetOGISDefn</span><span class="o">.</span><span class="n">restype</span> <span class="o">=</span> <span class="n">ctypes</span><span class="o">.</span><span class="n">c_char_p</span>

<span class="c"># Link GTIFSetFromOGISDefn</span>
<span class="n">GTIFSetFromOGISDefn</span> <span class="o">=</span> <span class="n">lib</span><span class="o">.</span><span class="n">GTIFSetFromOGISDefn</span>
<span class="n">GTIFSetFromOGISDefn</span><span class="o">.</span><span class="n">argtypes</span> <span class="o">=</span> <span class="p">[</span><span class="n">ctypes</span><span class="o">.</span><span class="n">POINTER</span><span class="p">(</span><span class="n">GTiff</span><span class="p">),</span>
  <span class="n">ctypes</span><span class="o">.</span><span class="n">POINTER</span><span class="p">(</span><span class="n">ctypes</span><span class="o">.</span><span class="n">c_char</span><span class="p">)]</span>
<span class="n">GTIFSetFromOGISDefn</span><span class="o">.</span><span class="n">restype</span> <span class="o">=</span> <span class="n">ctypes</span><span class="o">.</span><span class="n">c_int</span>

<span class="c"># Link GTIFNew</span>
<span class="n">GTIFNew</span> <span class="o">=</span> <span class="n">lib</span><span class="o">.</span><span class="n">GTIFNew</span>
<span class="n">GTIFNew</span><span class="o">.</span><span class="n">argtypes</span> <span class="o">=</span> <span class="p">[</span><span class="n">ctypes</span><span class="o">.</span><span class="n">c_void_p</span><span class="p">]</span>
<span class="n">GTIFNew</span><span class="o">.</span><span class="n">restype</span> <span class="o">=</span> <span class="n">ctypes</span><span class="o">.</span><span class="n">POINTER</span><span class="p">(</span><span class="n">GTiff</span><span class="p">)</span>

<span class="c">#-------------------------------------------------------------------------------</span>
<span class="c"># Link `Python File` as `File`</span>
<span class="n">PyFile_AsFile</span> <span class="o">=</span> <span class="n">ctypes</span><span class="o">.</span><span class="n">pythonapi</span><span class="o">.</span><span class="n">PyFile_AsFile</span>
<span class="n">PyFile_AsFile</span><span class="o">.</span><span class="n">argtypes</span> <span class="o">=</span> <span class="p">[</span><span class="n">ctypes</span><span class="o">.</span><span class="n">py_object</span><span class="p">]</span>
<span class="n">PyFile_AsFile</span><span class="o">.</span><span class="n">restype</span> <span class="o">=</span> <span class="n">pFile</span>

<span class="c">#-------------------------------------------------------------------------------</span>
<span class="c"># Get stdout</span>
<span class="n">stdout_file</span> <span class="o">=</span> <span class="n">PyFile_AsFile</span><span class="p">(</span><span class="n">sys</span><span class="o">.</span><span class="n">stdout</span><span class="p">)</span>

<span class="c">#-------------------------------------------------------------------------------</span>
<div class="viewcode-block" id="geoKeysFromWkt"><a class="viewcode-back" href="../../ALASpy.html#ALASpy.geotif.geoKeysFromWkt">[docs]</a><span class="k">def</span> <span class="nf">geoKeysFromWkt</span><span class="p">(</span><span class="n">wkt</span><span class="p">):</span>
  <span class="sd">&quot;&quot;&quot; Return geoKeys tuple, geoDoubleParams and geoAsciiParams from wkt string</span>

<span class="sd">  :returns: (GeoKeys, GeoDoubleParams, GeoAsciiParams)</span>
<span class="sd">  :rtype: tuple</span>
<span class="sd">  &quot;&quot;&quot;</span>
  <span class="c"># Create e new (empty) GTiff structure</span>
  <span class="n">pNewGtif</span> <span class="o">=</span> <span class="n">GTIFNew</span><span class="p">(</span><span class="n">ctypes</span><span class="o">.</span><span class="n">c_void_p</span><span class="p">())</span>

  <span class="c"># Set wkt string to the new object</span>
  <span class="n">rc</span> <span class="o">=</span> <span class="n">GTIFSetFromOGISDefn</span><span class="p">(</span><span class="n">pNewGtif</span><span class="p">,</span> <span class="n">wkt</span><span class="p">)</span>
  <span class="k">if</span> <span class="ow">not</span> <span class="n">rc</span><span class="p">:</span>
    <span class="k">raise</span> <span class="n">ConversionFailed</span><span class="p">(</span>
      <span class="s">&quot;GTIFSetFromOGISDefn failed with WKT = &#39;</span><span class="si">%s</span><span class="s">&#39;&quot;</span> <span class="o">%</span> <span class="n">wkt</span><span class="p">)</span>

  <span class="n">gtif</span> <span class="o">=</span> <span class="n">pNewGtif</span><span class="o">.</span><span class="n">contents</span>
  <span class="k">return</span> <span class="n">gtif</span><span class="o">.</span><span class="n">geoKeys</span><span class="p">()</span>

<span class="c">#-------------------------------------------------------------------------------</span></div>
<div class="viewcode-block" id="normalize"><a class="viewcode-back" href="../../ALASpy.html#ALASpy.geotif.normalize">[docs]</a><span class="k">def</span> <span class="nf">normalize</span><span class="p">(</span><span class="n">geoKeyDirectoryTag</span><span class="p">,</span> <span class="n">geoDoubleParamsTag</span><span class="o">=</span><span class="bp">None</span><span class="p">,</span>
  <span class="n">geoASCIIParamsTag</span><span class="o">=</span><span class="bp">None</span><span class="p">):</span>
  <span class="sd">&quot;&quot;&quot; Return &quot;normalized&quot; (i.e. translated into WKT) version of geotiff tags</span>
<span class="sd">      found in variable length records of a LAS file</span>

<span class="sd">      See gdal-1.9.0/frmts/gtiff/libgeotiff/geo_normalize.c</span>

<span class="sd">  :param geoKeyDirectoryTag: Mandatory GeoKeyDirectoryTag</span>
<span class="sd">  :type geoKeyDirectoryTag: :class:`VLRecord` instance</span>
<span class="sd">  :param geoDoubleParamsTag: GeoDoubleParamsTag</span>
<span class="sd">  :type geoDoubleParamsTag: :class:`VLRecord` instance</span>
<span class="sd">  :param geoASCIIParamsTag: GeoASCIIParamsTag</span>
<span class="sd">  :type geoASCIIParamsTag: :class:`VLRecord` instance</span>
<span class="sd">  :returns: wkt</span>
<span class="sd">  :rtype: string</span>
<span class="sd">  &quot;&quot;&quot;</span>
  <span class="n">geoKeys</span> <span class="o">=</span> <span class="n">geoKeyDirectoryTag</span><span class="o">.</span><span class="n">getData</span><span class="p">()</span>
  <span class="k">if</span> <span class="n">geoDoubleParamsTag</span><span class="p">:</span>
    <span class="n">geoDoubleParams</span> <span class="o">=</span> <span class="n">geoDoubleParamsTag</span><span class="o">.</span><span class="n">getData</span><span class="p">()</span>
  <span class="k">else</span><span class="p">:</span>
    <span class="n">geoDoubleParams</span> <span class="o">=</span> <span class="bp">None</span>
  <span class="k">if</span> <span class="n">geoASCIIParamsTag</span><span class="p">:</span>
    <span class="n">geoAsciiParams</span> <span class="o">=</span> <span class="n">geoASCIIParamsTag</span><span class="o">.</span><span class="n">getData</span><span class="p">()</span>
  <span class="k">else</span><span class="p">:</span>
    <span class="n">geoAsciiParams</span> <span class="o">=</span> <span class="bp">None</span>

  <span class="c"># Create GTiff tags</span>
  <span class="n">gt</span> <span class="o">=</span> <span class="n">GTiff</span><span class="p">(</span><span class="n">geoKeys</span><span class="p">,</span> <span class="n">geoAsciiParams</span><span class="o">=</span><span class="n">geoAsciiParams</span><span class="p">,</span>
    <span class="n">geoDoubleParams</span><span class="o">=</span><span class="n">geoDoubleParams</span><span class="p">)</span>

  <span class="c"># Normalize GTiff tags</span>
  <span class="n">defn</span> <span class="o">=</span> <span class="n">GTIFDefn</span><span class="p">()</span>
  <span class="n">rc</span> <span class="o">=</span> <span class="n">GTIFGetDefn</span><span class="p">(</span><span class="n">ctypes</span><span class="o">.</span><span class="n">byref</span><span class="p">(</span><span class="n">gt</span><span class="p">),</span> <span class="n">ctypes</span><span class="o">.</span><span class="n">byref</span><span class="p">(</span><span class="n">defn</span><span class="p">))</span>

  <span class="c">#print defn</span>
  <span class="k">if</span> <span class="ow">not</span> <span class="n">rc</span><span class="p">:</span>
    <span class="k">raise</span> <span class="n">ConversionFailed</span><span class="p">(</span>
      <span class="s">&quot;GTIFGetDefn failed converting &#39;</span><span class="si">%s</span><span class="s">&#39;&quot;</span> <span class="o">%</span> <span class="nb">repr</span><span class="p">(</span><span class="n">gt</span><span class="p">))</span>

  <span class="c"># Get wkt string from gtif structure (GTiff tags)</span>
  <span class="n">wkt</span> <span class="o">=</span> <span class="n">GTIFGetOGISDefn</span><span class="p">(</span><span class="n">ctypes</span><span class="o">.</span><span class="n">byref</span><span class="p">(</span><span class="n">gt</span><span class="p">),</span> <span class="n">ctypes</span><span class="o">.</span><span class="n">byref</span><span class="p">(</span><span class="n">defn</span><span class="p">))</span>
  <span class="k">return</span> <span class="n">wkt</span>

<span class="c">#-------------------------------------------------------------------------------</span></div>
<span class="k">if</span> <span class="n">__name__</span> <span class="o">==</span> <span class="s">&#39;__main__&#39;</span><span class="p">:</span>
  <span class="c"># The Geo Keys:</span>
  <span class="n">geoKeys</span> <span class="o">=</span> <span class="p">[</span>
  <span class="p">(</span><span class="mi">1</span><span class="p">,</span> <span class="mi">1</span><span class="p">,</span> <span class="mi">0</span><span class="p">,</span> <span class="mi">13</span><span class="p">),</span>       <span class="c"># a Version 1 GeoTIFF GeoKey, Rev. 1.0, 13 Keys</span>
  <span class="p">(</span><span class="mi">1024</span><span class="p">,</span> <span class="mi">0</span><span class="p">,</span> <span class="mi">1</span><span class="p">,</span> <span class="mi">1</span><span class="p">),</span>     <span class="c"># TIFFTagLocation = 0 count = 1 Projected = 1</span>
  <span class="p">(</span><span class="mi">2048</span><span class="p">,</span> <span class="mi">0</span><span class="p">,</span> <span class="mi">1</span><span class="p">,</span> <span class="mi">32767</span><span class="p">),</span>
  <span class="p">(</span><span class="mi">2050</span><span class="p">,</span> <span class="mi">0</span><span class="p">,</span> <span class="mi">1</span><span class="p">,</span> <span class="mi">32767</span><span class="p">),</span>
  <span class="p">(</span><span class="mi">2056</span><span class="p">,</span> <span class="mi">0</span><span class="p">,</span> <span class="mi">1</span><span class="p">,</span> <span class="mi">7030</span><span class="p">),</span>
  <span class="p">(</span><span class="mi">3072</span><span class="p">,</span> <span class="mi">0</span><span class="p">,</span> <span class="mi">1</span><span class="p">,</span> <span class="mi">32767</span><span class="p">),</span>
  <span class="p">(</span><span class="mi">3074</span><span class="p">,</span> <span class="mi">0</span><span class="p">,</span> <span class="mi">1</span><span class="p">,</span> <span class="mi">32767</span><span class="p">),</span>
  <span class="p">(</span><span class="mi">3075</span><span class="p">,</span> <span class="mi">0</span><span class="p">,</span> <span class="mi">1</span><span class="p">,</span> <span class="mi">1</span><span class="p">),</span>
  <span class="p">(</span><span class="mi">3076</span><span class="p">,</span> <span class="mi">0</span><span class="p">,</span> <span class="mi">1</span><span class="p">,</span> <span class="mi">9001</span><span class="p">),</span>
  <span class="p">(</span><span class="mi">3080</span><span class="p">,</span> <span class="mi">34736</span><span class="p">,</span> <span class="mi">1</span><span class="p">,</span> <span class="mi">1</span><span class="p">),</span> <span class="c"># GeoDoubleParamsTag = 34736, count = 1 index = 1</span>
  <span class="p">(</span><span class="mi">3081</span><span class="p">,</span> <span class="mi">34736</span><span class="p">,</span> <span class="mi">1</span><span class="p">,</span> <span class="mi">0</span><span class="p">),</span> <span class="c"># GeoDoubleParamsTag = 34736, count = 1 index = 0</span>
  <span class="p">(</span><span class="mi">3082</span><span class="p">,</span> <span class="mi">34736</span><span class="p">,</span> <span class="mi">1</span><span class="p">,</span> <span class="mi">3</span><span class="p">),</span> <span class="c"># GeoDoubleParamsTag = 34736, count = 1 index = 3</span>
  <span class="p">(</span><span class="mi">3083</span><span class="p">,</span> <span class="mi">34736</span><span class="p">,</span> <span class="mi">1</span><span class="p">,</span> <span class="mi">4</span><span class="p">),</span>
  <span class="p">(</span><span class="mi">3092</span><span class="p">,</span> <span class="mi">34736</span><span class="p">,</span> <span class="mi">1</span><span class="p">,</span> <span class="mi">2</span><span class="p">)</span>
  <span class="p">]</span>
  <span class="n">geoDoubles</span> <span class="o">=</span> <span class="p">(</span><span class="mf">0.0</span><span class="p">,</span> <span class="o">-</span><span class="mf">81.0</span><span class="p">,</span> <span class="mf">0.9996</span><span class="p">,</span> <span class="mf">500000.0</span><span class="p">,</span> <span class="mf">0.0</span><span class="p">)</span>
  <span class="n">gtif</span> <span class="o">=</span> <span class="n">GTiff</span><span class="p">(</span><span class="n">geoKeys</span><span class="p">,</span> <span class="n">geoDoubleParams</span><span class="o">=</span><span class="n">geoDoubles</span><span class="p">)</span>
  <span class="c"># Normalize GTiff tags</span>
  <span class="n">defn</span> <span class="o">=</span> <span class="n">GTIFDefn</span><span class="p">()</span>
  <span class="n">rc</span> <span class="o">=</span> <span class="n">GTIFGetDefn</span><span class="p">(</span><span class="n">ctypes</span><span class="o">.</span><span class="n">byref</span><span class="p">(</span><span class="n">gtif</span><span class="p">),</span> <span class="n">ctypes</span><span class="o">.</span><span class="n">byref</span><span class="p">(</span><span class="n">defn</span><span class="p">))</span>
  <span class="k">if</span> <span class="ow">not</span> <span class="n">rc</span><span class="p">:</span>
    <span class="k">raise</span> <span class="n">ConversionFailed</span><span class="p">(</span>
      <span class="s">&quot;GTIFGetDefn failed converting &#39;</span><span class="si">%s</span><span class="s">&#39;&quot;</span> <span class="o">%</span> <span class="nb">repr</span><span class="p">(</span><span class="n">gtif</span><span class="p">))</span>

  <span class="c"># Get wkt string from gtif structure (GTiff tags)</span>
  <span class="n">wkt</span> <span class="o">=</span> <span class="n">GTIFGetOGISDefn</span><span class="p">(</span><span class="n">ctypes</span><span class="o">.</span><span class="n">byref</span><span class="p">(</span><span class="n">gtif</span><span class="p">),</span> <span class="n">ctypes</span><span class="o">.</span><span class="n">byref</span><span class="p">(</span><span class="n">defn</span><span class="p">))</span>
  <span class="k">print</span> <span class="s">&quot;</span><span class="se">\n\n</span><span class="s">WKT from geokeys and GeoDoubles:</span><span class="se">\n</span><span class="s">&quot;</span><span class="p">,</span> <span class="n">wkt</span>
  <span class="k">print</span> <span class="s">&quot;Creating now a new object from this WKT string...&quot;</span><span class="p">,</span>

  <span class="c"># Create e new (empty) GTiff structure</span>
  <span class="n">pNewGtif</span> <span class="o">=</span> <span class="n">GTIFNew</span><span class="p">(</span><span class="n">ctypes</span><span class="o">.</span><span class="n">c_void_p</span><span class="p">())</span>

  <span class="c"># Set wkt string to the new object</span>
  <span class="n">rc</span> <span class="o">=</span> <span class="n">GTIFSetFromOGISDefn</span><span class="p">(</span><span class="n">pNewGtif</span><span class="p">,</span> <span class="n">wkt</span><span class="p">)</span>
  <span class="k">if</span> <span class="ow">not</span> <span class="n">rc</span><span class="p">:</span>
    <span class="k">raise</span> <span class="n">ConversionFailed</span><span class="p">(</span>
      <span class="s">&quot;GTIFSetFromOGISDefn failed with WKT = &#39;</span><span class="si">%s</span><span class="s">&#39;&quot;</span> <span class="o">%</span> <span class="n">wkt</span><span class="p">)</span>

  <span class="c"># Normalize GTiff tags</span>
  <span class="n">defnNew</span> <span class="o">=</span> <span class="n">GTIFDefn</span><span class="p">()</span>
  <span class="n">rc</span> <span class="o">=</span> <span class="n">GTIFGetDefn</span><span class="p">(</span><span class="n">pNewGtif</span><span class="p">,</span> <span class="n">ctypes</span><span class="o">.</span><span class="n">byref</span><span class="p">(</span><span class="n">defnNew</span><span class="p">))</span>
  <span class="k">if</span> <span class="ow">not</span> <span class="n">rc</span><span class="p">:</span>
    <span class="k">raise</span> <span class="n">ConversionFailed</span><span class="p">(</span>
      <span class="s">&quot;GTIFGetDefn failed converting &#39;</span><span class="si">%s</span><span class="s">&#39;&quot;</span> <span class="o">%</span> <span class="nb">repr</span><span class="p">(</span><span class="n">defnNew</span><span class="p">))</span>

  <span class="c"># It worked !</span>
  <span class="k">print</span> <span class="s">&quot;done.&quot;</span>
  <span class="k">print</span> <span class="s">&quot;</span><span class="se">\n</span><span class="s">This is the new object structure printed by GTIFPrintDefn:&quot;</span>
  <span class="n">GTIFPrintDefn</span><span class="p">(</span><span class="n">ctypes</span><span class="o">.</span><span class="n">byref</span><span class="p">(</span><span class="n">defnNew</span><span class="p">),</span> <span class="n">stdout_file</span><span class="p">)</span>
  <span class="n">wktNew</span> <span class="o">=</span> <span class="n">GTIFGetOGISDefn</span><span class="p">(</span><span class="n">pNewGtif</span><span class="p">,</span> <span class="n">ctypes</span><span class="o">.</span><span class="n">byref</span><span class="p">(</span><span class="n">defnNew</span><span class="p">))</span>
  <span class="k">print</span> <span class="s">&quot;</span><span class="se">\n</span><span class="s">wkt == wktNew is &#39;</span><span class="si">%s</span><span class="s">&#39;&quot;</span> <span class="o">%</span> <span class="nb">str</span><span class="p">(</span><span class="n">wkt</span><span class="o">==</span><span class="n">wktNew</span><span class="p">)</span>

  <span class="n">geoKeysFromWkt</span><span class="p">(</span><span class="n">wktNew</span><span class="p">)</span>
</pre></div>

          </div>
        </div>
      </div>
      <div class="sphinxsidebar">
        <div class="sphinxsidebarwrapper">
            <p class="logo"><a href="../../index.html">
              <img class="logo" src="../../_static/ogs.png" alt="Logo"/>
            </a></p>
<div id="searchbox" style="display: none">
  <h3>Quick search</h3>
    <form class="search" action="../../search.html" method="get">
      <input type="text" name="q" />
      <input type="submit" value="Go" />
      <input type="hidden" name="check_keywords" value="yes" />
      <input type="hidden" name="area" value="default" />
    </form>
    <p class="searchtip" style="font-size: 90%">
    Enter search terms or a module, class or function name.
    </p>
</div>
<script type="text/javascript">$('#searchbox').show(0);</script>
        </div>
      </div>
      <div class="clearer"></div>
    </div>
    <div class="related">
      <h3>Navigation</h3>
      <ul>
        <li class="right" style="margin-right: 10px">
          <a href="../../genindex.html" title="General Index"
             >index</a></li>
        <li class="right" >
          <a href="../../py-modindex.html" title="Python Module Index"
             >modules</a> |</li>
        <li><a href="../../index.html">ALASpy 0.1.1 documentation</a> &raquo;</li>
          <li><a href="../index.html" >Module code</a> &raquo;</li> 
      </ul>
    </div>
    <div class="footer">
        &copy; Copyright 2012, Roberto Vidmar, Nicola Creati.
      Created using <a href="http://sphinx.pocoo.org/">Sphinx</a> 1.1.3.
    </div>
  </body>
</html>